<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Signin
 *
 * @author peerawatj20
 */
class signin extends MY_Controller {

    //put your code here
    public function index() {
        $this->form_validation->set_rules('txtUsername', 'Username', 'required|trim');
        $this->form_validation->set_rules('txtPassword', 'Password', 'required');
        $username = $this->input->post('txtUsername');
        $password = base64_encode(md5($this->input->post('txtPassword')));

        $this->load->model('signin_model');

        if ($this->form_validation->run() != FALSE) {
            $result = $this->signin_model->check_username($username, $password);

            if (count($result) > 0) {
                $new_session = array(
                    'username' => $result[0]->username,
                    'logged_in' => TRUE
                );
                $this->session->set_userdata($new_session);
                $this->signin_model->insert_log_login($result[0]->username);
                redirect('home');
                exit();
            }
        }

        redirect('portal');
    }

}
